package com.ocean.courtyard;

import com.ocean.courtyard.command.MasterCommand;
import com.ocean.courtyard.core.datasource.SqlSessionImpl;
import com.ocean.courtyard.core.datasource.config.MyBatisConfig;
import com.ocean.courtyard.core.hook.HuskHomeApiHook;
import com.ocean.courtyard.core.worldguard.WorldGuardSetting;
import com.ocean.courtyard.listener.WorldLimitListener;
import com.ocean.courtyard.message.Message;
import com.ocean.courtyard.util.FileUtils;
import com.onarandombox.MultiverseCore.MultiverseCore;
import com.sk89q.worldguard.WorldGuard;
import lombok.Getter;
import org.bukkit.Bukkit;
import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.configuration.file.YamlConfiguration;
import org.bukkit.plugin.java.JavaPlugin;

import java.io.File;
import java.io.IOException;

public final class CourtyardApplication extends JavaPlugin {

    @Getter
    private final String pluginChannel = "courtyard:requestWorldCount";
    @Getter
    private static CourtyardApplication instance;
    @Getter
    private MultiverseCore core;
    @Getter
    private SqlSessionImpl sqlSession;
    @Getter
    private HuskHomeApiHook huskHomesHook;
    @Getter
    private Message message;

    private WorldGuardSetting worldGuardSetting;

    @Override
    public void onEnable() {
        getLogger().info("Courtyard Enable...");
        instance = this;
        WorldGuard worldGuard = WorldGuard.getInstance();

        core = (MultiverseCore) Bukkit.getServer().getPluginManager().getPlugin("Multiverse-Core");

        this.initConfig();

        FileConfiguration fileConfiguration = loadMessagesConfig();
        this.message = new Message(getConfig(), fileConfiguration);

        this.huskHomesHook = new HuskHomeApiHook(message);
        worldGuardSetting = new WorldGuardSetting(worldGuard);

        MyBatisConfig myBatisConfig = new MyBatisConfig(this);
        sqlSession = myBatisConfig.getSqlSessionImpl();

        WorldLimitListener worldLimitListener = new WorldLimitListener();
        getServer().getPluginManager().registerEvents(worldLimitListener, this);

        this.getCommand("cy").setExecutor(new MasterCommand(this, sqlSession, message, huskHomesHook, worldGuardSetting));

        getLogger().info("Courtyard Done!");
    }

    @Override
    public void onDisable() {
        getLogger().info("Courtyard Disable...");
    }

    private void initConfig() {
        FileConfiguration config = this.getConfig();
        this.saveDefaultConfig();
        String templateFolder = FileUtils.getTemplateFolder(config);
        File dataFolder = this.getDataFolder();
        if (!dataFolder.exists()) {
            dataFolder.mkdir();
        }
        File worldsFolder = new File(dataFolder, templateFolder);
        if (!worldsFolder.exists()) {
            worldsFolder.mkdir();
        }
        File playerWorldFolder = new File(Bukkit.getWorldContainer(), FileUtils.getWorldContainer(config));
        if (!playerWorldFolder.exists()) {
            playerWorldFolder.mkdir();
        }
    }

    private FileConfiguration loadMessagesConfig() {
        // 获取messages.yml文件的File对象
        File messagesFile = new File(getDataFolder(), "messages.yml");
        // 检查文件是否存在，如果不存在则保存默认配置
        if (!messagesFile.exists()) {
            this.saveResource("messages.yml", false);
        }
        // 加载配置文件
        YamlConfiguration yamlConfiguration = new YamlConfiguration();
        try {
            yamlConfiguration.load(messagesFile);
        } catch (IOException | org.bukkit.configuration.InvalidConfigurationException e) {
            getLogger().severe("Couldn't load messages.yml");
        }
        return yamlConfiguration;
    }
}
